package cn.ztydata.teachingsystem.heaven.dao.redis;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 习题->资源 redis类
 * Created by chenxin on 2015/02/04.
 */
@Repository
public class ExerciseResDao extends BaseRedisDao {
    private static final String EXERCISE_RES = "exercise_res";
    static final String RES_EXERCISE = "res_exercise";

    /**
     * 保存习题与资源的关系
     * @param exerciseId 习题ID
     * @param resId 资源ID
     * @return Boolean
     *
     * @author cx
     * @since 2015-02-04
     */
    public Boolean saveRes(final long exerciseId, final long resId) {
        SessionCallback<Boolean> sessionCallback = new SessionCallback<Boolean>() {
            @Override
            public Boolean execute(RedisOperations operations) throws DataAccessException {
                operations.multi();

                //获取List操作
                ListOperations<String, Long> listOperations = operations.opsForList();
                //获取Set操作
                SetOperations<String, Long> setOperations = operations.opsForSet();

                //保存习题->资源的关系
                listOperations.leftPush(KeyUtils.generateKey(EXERCISE_RES, exerciseId), resId);
                //保存资源->习题的关系
                setOperations.add(KeyUtils.generateKey(RES_EXERCISE, resId), exerciseId);

                operations.exec();
                return true;
            }
        };

        logger.debug("保存习题与资源关系成功");
        return (Boolean)redisTemplate.execute(sessionCallback);
    }

    /**
     * 获取习题的所有资源ID
     * @param exerciseId 习题ID
     * @return List
     *
     * @author chenxin
     * @since 2015/02/04
     */
    public List<Long> getResIds(final long exerciseId) {
        ListOperations<String, Long> listOperations = redisTemplate.opsForList();

        logger.debug("获取习题的所有资源ID");
        return listOperations.range(KeyUtils.generateKey(EXERCISE_RES, exerciseId), 0, -1);
    }

    /**
     * 分页获取资源ID
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @param exerciseId 习题ID
     *
     * @author chenxin
     * @since 2015/02/04
     */
    public List<Long> getResIdsForPage(final int pageNum, final int pageSize, final long exerciseId) {
        ListOperations<String, Long> listOperations = redisTemplate.opsForList();

        return listOperations.range(KeyUtils.generateKey(EXERCISE_RES, exerciseId), pageSize * (pageNum - 1), pageNum * pageSize - 1);
    }

    /**
     * 获取资源ID总数
     *
     * @author chenxin
     * @since 2015/02/04
     */
    public Long getTotal(final long exerciseId) {
        ListOperations<String, Long> listOperations = redisTemplate.opsForList();

        logger.debug("获取资源ID总数");
        return listOperations.size(KeyUtils.generateKey(EXERCISE_RES, exerciseId));
    }
}
